#include "PatternView.h"

#include <iostream>

/*-----------------------------------------------------------------------------
 *  Constrói um visualizador de padrões
 *-----------------------------------------------------------------------------*/
PatternView::PatternView(QWidget *parent) :
        QGraphicsView(parent)
{
    setAlignment(Qt::AlignLeft | Qt::AlignTop);
    scene = NULL;
}

/*-----------------------------------------------------------------------------
 *  Adiciona a grip dado seu tamanho
 *-----------------------------------------------------------------------------*/
void PatternView::setGrid(uint gridSize)
{
    this->gridSize = gridSize;

    uint xDimension = width() - 2;
    uint yDimension = height() - 2;

    stepx = (xDimension / (float) gridSize);
    stepy = (yDimension / (float) gridSize);

    scene = new QGraphicsScene();
    setScene(scene);

    float x0 = stepx, x1 = stepx, y0 = 0, y1 = yDimension;

    for(uint i = 0; i < gridSize - 1; i++)
    {
        (scene)->addLine(x0, y0, x1, y1);
        x0 += stepx;
        x1 += stepx;
    }

    y0 = stepy, y1 = stepy, x0 = 0, x1 = xDimension;

    for(uint i = 0; i < gridSize - 1; i++)
    {
        (scene)->addLine(x0, y0, x1, y1);
        y0 += stepy;
        y1 += stepy;
    }
}

/*-----------------------------------------------------------------------------
 *  Pinta o quadrado dado pelas posições (x, y)
 *-----------------------------------------------------------------------------*/
void PatternView::set(uint x, uint y)
{
    scene->addRect((qreal) y * stepx, (qreal) x * stepy, stepx, stepy, QPen(), QBrush(QColor(Qt::black)));
}

/*-----------------------------------------------------------------------------
 *  Limpa o gráfico
 *-----------------------------------------------------------------------------*/
void PatternView::clear()
{
    if(scene != NULL)
        scene->clear();
}
